﻿using Ewelve_Four_HD.Model;

namespace Ewelve_Four_HD.DAL
{
    public class TypeDal
    {
        private readonly Mycontext context;

        public TypeDal(Mycontext _context)
        {
            context = _context;
        }
        /// <summary>
        /// 房态添加
        /// </summary>
        /// <param name="S"></param>
        /// <returns></returns>
        public int TypeAdd(RoomType S)
        {
            try
            {
                var T = context.RoomTypes.Count(R => R.TypeName == S.TypeName);
                if (T > 0)
                {
                    return -1;
                }
                context.RoomTypes.Add(S);
                return context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 房态查询分页
        /// </summary>
        /// <param name="TypeName"></param>
        /// <param name="RoomStage"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageSum"></param>
        /// <param name="PageCount"></param>
        /// <returns></returns>
        public IQueryable TypeShowAll(string? TypeName, int? RoomStage, int PageIndex, int PageSize, out int PageSum, out int PageCount)
        {
            var F=context.RoomTypes.AsQueryable();
            if(!string.IsNullOrEmpty(TypeName) )
            {
                F=F.Where(R => R.TypeName.Contains(TypeName));
            }
            if(RoomStage!=0)
            {
                F = F.Where(R => R.RoomStage == RoomStage);
            }
            PageSum = F.Count();
            PageCount = (int)Math.Ceiling((decimal)PageSum / PageSize);
            return F.OrderBy(S=>S.RoomId).Skip((PageIndex-1)*PageSize).Take(PageSize);
        }
    }
}
